package com.tust.app.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Utils {
    private static final String salt = "#h$6k@w1+8";

    public static String md5(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(input.getBytes());
            return bytesToHex(md.digest());
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    private static String bytesToHex(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) {
            sb.append(String.format("%02x", b));
        }
        return sb.toString();
    }

    public static String doubleMD5WithSalt(String str, String salt) {
        String firstMD5 = md5(str);
        return md5(firstMD5 + salt);
    }

    public static String md5PlusSalt(String keyword) {
        return doubleMD5WithSalt(keyword, salt);
    }

    public static void main(String[] args) throws Exception {
        String keyword = "1234";
        String md5salt = md5PlusSalt(keyword);
        System.out.println("加盐后：" + "\n" + md5salt);
    }
}
